package cn.hy.preparelesson.common.utils;


import cn.hy.preparelesson.common.entity.BusiGradeOption;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 班级Utils
 */
public class ClassUtils {

    /**
     * 获取格式化的年级信息（年类型）
     * @return
     */
    public static String getGradeName(int distanceYear, int grade, List<BusiGradeOption> gradeOptionList) {
        if (CollectionUtils.isEmpty(gradeOptionList)) {
            return grade + "级";
        }

        for (BusiGradeOption goTmp : gradeOptionList) {
            if (goTmp.getDistanceYear().compareTo(new BigDecimal(distanceYear)) == 0) {
                return goTmp.getName();
            }
        }

        return grade + "级";
    }

    /**
     * 获取格式化的班级名称
     * @return
     */
    public static String getGradeName(int nowYear, int nowMonth, int grade, List<BusiGradeOption> gradeOptionList) {
        int belongYear = getBelongYear(nowYear, nowMonth);
        return getGradeName(belongYear - grade, grade, gradeOptionList);
    }

    /**
     * 获取格式化的班级名称
     * @return
     */
    public static String getGradeName(Date now, int grade, List<BusiGradeOption> gradeOptionList) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String nowDate = format.format(now);
        String[] nowDateSplit = nowDate.split("-");
        int nowYear = Integer.parseInt(nowDateSplit[0]), nowMonth = Integer.parseInt(nowDateSplit[1]);
        return getGradeName(nowYear, nowMonth, grade, gradeOptionList);
    }


    /**
     * 获取格式化的班级名称信息
     * @return
     */
    public static String getClassName(int distanceYear, int grade, String classCode, List<BusiGradeOption> gradeOptionList) {
        String gradeName = getGradeName(distanceYear, grade, gradeOptionList);
        if (StringUtils.isNotBlank(classCode)) {
            return gradeName + "（" + classCode + "）班";
        } else {
            return gradeName;
        }
    }

    /**
     * 获取格式化的班级名称
     * @return
     */
    public static String getClassName(int nowYear, int nowMonth, int grade, String classCode, List<BusiGradeOption> gradeOptionList) {
        int belongYear = getBelongYear(nowYear, nowMonth);
        return getClassName(belongYear - grade, grade, classCode, gradeOptionList);
    }

    /**
     * 得到某个时间节点属于哪个年份学期，比如2014-08-01 -- 2015-07-31属于2014级
     * @param date
     * @return
     */
    public static int getBelongYear(Date date) {
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String nowDate = format.format(date);
        String[] nowDateSplit = nowDate.split("-");
        int nowYear = Integer.parseInt(nowDateSplit[0]), nowMonth = Integer.parseInt(nowDateSplit[1]);
        return getBelongYear(nowYear, nowMonth);
    }


    /**
     * 获取年级年份信息
     * @param date
     * @return
     */
    public static int getGradeByDistanceYear(int distanceYear, Date date) {
        int belongYear = getBelongYear(date);
        return belongYear - distanceYear;
    }


    /**
     * 得到某个时间节点属于哪个年份学期，比如2014-08-01 -- 2015-07-31属于2014级
     * @return
     */
    public static int getBelongYear(int nowYear, int nowMonth) {
        Integer belongYear = null;
        //以8月1号0时记升级点
        if (nowMonth < 8) {
            belongYear = nowYear - 1;
        } else {
            belongYear = nowYear;
        }
        return belongYear;
    }

    /**
     * 获取距离年数
     * @return
     */
    public static Integer getDistanceYear(Integer grade, Date now) {
        int belongYear = getBelongYear(now);
        return belongYear - grade;
    }

}
